package cn.xfdzx.android.apps.shop.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.login.OneLoginMainActivity;
import cn.xfdzx.android.apps.shop.bean.AddressListBeen;
import cn.xfdzx.android.apps.shop.bean.VideoRoomGoodListBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.presenter.HomeMarketPresenter;
import cn.xfdzx.android.apps.shop.presenter.VideoInfoPresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.UtilsData;
import cn.xfdzx.android.apps.shop.view.ShoppingCartAnimationView;

import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;

/**
 * @author ln
 * @date 2020/3/27
 * description：
 */
public class VideoUserGoodListAdapter extends RecyclerView.Adapter<VideoUserGoodListAdapter.MyViewHolder> {
    private ItemClickListener mItemClickListener;
    private List<VideoRoomGoodListBeen.DataBean.GoodsBean> mList;
    private Activity mContext;
    private int endPosition[];
    private String liveId;//直播间id
    private VideoInfoPresenter mPresenter;
    private OnAddCartClickListener onAddCartClickListener;

    public VideoUserGoodListAdapter(Activity context, List<VideoRoomGoodListBeen.DataBean.GoodsBean> list, VideoInfoPresenter marketPresenter) {
        this.mList = list;
        this.mContext = context;
        this.mPresenter = marketPresenter;
    }

    public void setPosition(int endPosition[], String liveId) {
        this.endPosition = endPosition;
        this.liveId = liveId;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_video_user_room_good_list, parent, false);
        MyViewHolder myHolder = new MyViewHolder(inflate);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
        RoundedCorners roundedCorners = new RoundedCorners(20);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
        Glide.with(mContext).load(mList.get(position).getThumb()).apply(options.transform(new CenterCrop(), roundedCorners)).into(holder.ivGoodImage);
        holder.tvGoodName.setText(mList.get(position).getTitle());

        if (Double.parseDouble(mList.get(position).getDiscountprice()) == 0) {
            holder.tvGoodPrice.setText("¥" + mList.get(position).getMarketprice() + "");
        } else {
            holder.tvGoodPrice.setText("¥" + mList.get(position).getDiscountprice() + "");
        }
        holder.ivCar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EmptyUtil.isEmpty(APP_TOKEN)) {
                    Intent intentlogin = new Intent(mContext, OneLoginMainActivity.class);
                    mContext.startActivity(intentlogin);
                    return;
                }
                ShoppingCartAnimationView shoppingCartAnimationView = new ShoppingCartAnimationView(mContext);
                shoppingCartAnimationView.setVideoColor();
                int startPostion[] = new int[2];
                holder.ivCar.getLocationInWindow(startPostion);
                shoppingCartAnimationView.setStartPosition(new Point(startPostion[0], startPostion[1]));
                ViewGroup rootView = (ViewGroup) mContext.getWindow().getDecorView();
                rootView.addView(shoppingCartAnimationView);
                shoppingCartAnimationView.setEndPosition(new Point(endPosition[0], endPosition[1]));
                shoppingCartAnimationView.startBeizerAnimation();
                mPresenter.addCar("0", "", mList.get(position).getId() + "", "1", "1", liveId);
                onAddCartClickListener.onAddCartClick(position);
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemClickListener.onItemClick(position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_good_image)
        ImageView ivGoodImage;
        @BindView(R.id.tv_good_name)
        TextView tvGoodName;
        @BindView(R.id.iv_car)
        ImageView ivCar;
        @BindView(R.id.tv_good_price)
        TextView tvGoodPrice;


        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }


    public interface OnAddCartClickListener {
        void onAddCartClick(int position);
    }

    public void setAddCartClickkListener(OnAddCartClickListener onAddCartClickListener) {
        this.onAddCartClickListener = onAddCartClickListener;

    }

}
